package cn.wolfcode.lww.controller;

import cn.wolfcode.lww.domain.Place;
import cn.wolfcode.lww.domain.User;
import cn.wolfcode.lww.service.PlaceService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
public class PlaceController {
    @Autowired
    PlaceService placeService;
    @RequestMapping("/getPlaceAll")
public String getPlaceAll(Model model){
        List<Place> places = placeService.selectAll();
        model.addAttribute("places",places);


        return "place/place";
    }
    @RequestMapping("/placeplus")
    public String placeplus(String type,Integer id,Model model)
    {
        if (type!=null){
            Place place = placeService.selectByPrimaryKey(id);
            model.addAttribute("place",place);
        }
        return "place/place_plus";
    }
    @RequestMapping("/insertPlace")
    public String insertPlace(String place, String intrude, MultipartFile files, HttpSession session,Integer tid){
        if(tid!=null){
            return "redirect:/updateByPrimaryKey";
        }
        User user=(User) session.getAttribute("user");
        Integer id = user.getId();
        if (files==null){
            System.out.println("null");
        }
        HashMap<String, String> map = new HashMap<String,String>();
        String url = "http://localhost:8080/cover/"+id+"/";
        String fileType= FilenameUtils.getExtension(files.getOriginalFilename());
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        String newFileName=uuid+"."+fileType;
        try {
            File fileDirectory=new File("D://file//"+id);
            //如果目录不存在，则创建路径
            if(!fileDirectory.exists()) fileDirectory.mkdirs();
            //写入文件
            files.transferTo(new File("D:/file/"+id+"/",newFileName));
            map.put("link", url+newFileName);
        } catch (Exception e) {

        }
        Place place1 = new Place();
        place1.setImage("http://localhost:8080/cover/"+id+"/"+newFileName);
        place1.setIntruduce(intrude);
        place1.setPlace(place);
        placeService.insert(place1);
        return "redirect:/getPlaceAll";
    }
    @RequestMapping("/deleteByPrimaryKey")
    public String deleteByPrimaryKey(Integer id){
        placeService.deleteByPrimaryKey(id);
        return "redirect:/getPlaceAll";
    }
    @RequestMapping("/updateByPrimaryKey")
    public String updateByPrimaryKey(String place, String intrude,Integer tid, MultipartFile files, HttpSession session){
        User user=(User) session.getAttribute("user");
        Integer id = user.getId();
        if (files==null){
            System.out.println("null");
        }
        HashMap<String, String> map = new HashMap<String,String>();
        String url = "http://localhost:8080/cover/"+id+"/";
        String fileType= FilenameUtils.getExtension(files.getOriginalFilename());
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        String newFileName=uuid+"."+fileType;
        try {
            File fileDirectory=new File("D://file//"+id);
            //如果目录不存在，则创建路径
            if(!fileDirectory.exists()) fileDirectory.mkdirs();
            //写入文件
            files.transferTo(new File("D:/file/"+id+"/",newFileName));
            map.put("link", url+newFileName);
        } catch (Exception e) {

        }
        Place place1 = new Place();
        place1.setImage("http://localhost:8080/cover/"+id+"/"+newFileName);
        place1.setIntruduce(intrude);
        place1.setPlace(place);
        place1.setId(tid);
        placeService.updateByPrimaryKey(place1);
        return "place/place";
    }
}
